<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="会员昵称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入会员昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleSearch()"
          >全部</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleSearch(0)"
          >待处理</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleSearch(1)"
          >已处理</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="反馈内容" align="center" prop="feedbackContent" />
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.feedbackPhotoListLength == 1"
            style="width: 60px; height: 60px"
            :src="scope.row.feedbackPhotoList"
            :preview-src-list="[scope.row.feedbackPhotoList]"
          ></el-image>
          <el-image
            v-else
            style="width: 60px; height: 60px"
            :src="scope.row.feedbackPhotoList"
            @click="handleImageClick(scope.row.feedbackPhoto)"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="会员昵称" align="center" prop="memberNickname">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.memberNickname) }}
        </template>
      </el-table-column>
      <el-table-column label="会员手机" align="center" prop="memberPhone">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.memberPhone) }}
        </template>
      </el-table-column>
      <el-table-column
        label="提交时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column label="处理人" align="center" prop="updateByName">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.updateByName) }}
        </template>
      </el-table-column>
      <el-table-column
        label="处理时间"
        align="center"
        prop="updateTime"
        width="180"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? "待处理" : "已处理" }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="text"
            @click="handle(scope.row)"
            >处理</el-button
          >
          <el-button
            v-else
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="填写处理意见"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="处理意见" prop="handleOption">
          <el-input
            type="textarea"
            v-model="form.handleOption"
            placeholder="请输入处理意见"
          />
        </el-form-item>
        <el-form-item label="上传图片">
          <ImageUpload
            :value="platformPhoto"
            :limit="3"
            @input="handleFileSuccess"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="查看"
      :visible.sync="open2"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <div>
        <div>{{ management.feedbackContent }}</div>
        <div class="imglist">
          <el-image
            v-for="(item, index) in management.feedbackPhoto"
            :key="index"
            :src="item"
            :preview-src-list="[item]"
          ></el-image>
        </div>
      </div>
      <el-divider />
      <div>
        <div>平台反馈</div>
        <div>{{ management.handleOption }}</div>
        <div class="imglist">
          <el-image
            v-for="(item, index) in management.platformPhoto"
            :key="index"
            :src="item"
            :preview-src-list="[item]"
          ></el-image>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      title="查看图片"
      :visible.sync="openPhoto"
      width="500px"
      append-to-body
      style="margin-top: 13%"
      :close-on-click-modal="false"
    >
      <div>
        <div class="imglist">
          <el-image
            v-for="(item, index) in feedbackPhotoList"
            :key="index"
            :src="item"
            :preview-src-list="[item]"
          ></el-image>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, updateFeedback } from "@/api/system/post";

export default {
  name: "userRequirementManagement",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      postList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        memberName: undefined,
        handleOption: undefined,
        status: undefined,
      },
      form: {},
      rules: {
        handleOption: [
          { required: true, message: "处理意见不能为空", trigger: "blur" },
        ],
      },
      management: {
        feedbackContent: "",
        feedbackPhoto: [],
        handleOption: "",
        platformPhoto: [],
      },
      open2: false,
      platformPhoto: [],
      openPhoto: false,
      feedbackPhotoList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleImageClick(url) {
      this.openPhoto = true;
      this.feedbackPhotoList = url.split(",");
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      console.log("response: ", response);
      this.platformPhoto = response;
    },
    handle(row) {
      this.open = true;
      this.form = JSON.parse(JSON.stringify(row));
      this.platformPhoto = [];
    },
    handleView(row) {
      this.open2 = true;
      this.management = { ...row };
      this.management.feedbackPhoto = row.feedbackPhoto.split(",");
      this.management.platformPhoto = row.platformPhoto.slice(1, -1).split(",");
    },
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then((response) => {
        if (response.code == 200) {
          response.rows?.forEach((item) => {
            let feedbackPh = item.feedbackPhoto.split(",");
            item.feedbackPhotoListLength = feedbackPh.length;
            item.feedbackPhotoList = feedbackPh[0];
          });
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.form.status = 0;
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        platformPhoto: undefined,
        handleOption: undefined,
        phonenumber: 0,
        status: "0",
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSearch(v) {
      switch (v) {
        case "":
          this.queryParams.status = undefined;
          break;
        default:
          this.queryParams.status = v;
          break;
      }
      this.getList();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.platformPhoto.length == 0) {
            this.$message.warning("请上传图片");
            return;
          }
          this.form.status = 1;
          let p = {
            ...this.form,
            platformPhotos: this.platformPhoto.split(","),
          };
          updateFeedback(p).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("处理成功");
              this.open = false;
              this.getList();
            }
          });
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.imglist {
  display: flex;
  margin-top: 10px;
  flex-wrap: wrap;

  .el-image,
  img {
    width: 100px;
    height: 100px;
    margin-right: 10px;
  }
}
</style>